package com.ve.s2g.chain

import com.ve.s2g.pojo.Project
import com.ve.s2g.util.Constants
import com.ve.s2g.util.Shell
import com.ve.s2g.util.getPathInCP

class MoveSvnCodeToGit: AbstractHandler(){
    private val svnPathsMap by lazy {
        Constants.SVN_PATH_LIST.split("\\s*-\\s*".toRegex()).associate { svnUrl: String ->
        var name = svnUrl.trimEnd('/').split("/").reversed()[0]
        name to svnUrl
    } }
    override fun deal(dealObject: Any?) {

        when(dealObject){
            is List<*> -> {
                dealObject.forEach { it: Any? ->
                    moveSvn2Git(it as Project)
                    //getLogger<MoveSvnCodeToGit>().error(it.toString())
                }
            }
            is Project ->{
                moveSvn2Git(dealObject)
            }
            null ->{
                return
            }
            else -> {
                return
            }
        }
        if(dealObject!=null)
            nextHandler!!.deal(dealObject)
    }

    fun moveSvn2Git(project: Project){
        val codePath = getString4Cfg("local_code_path")
        val svn_relative_path = getString4Cfg("svn_relative_path")
        val local_alias_path = getString4Cfg("local_alias_path")
        val git_branch_name = getString4Cfg("git_branch_name")
        val svnPath="${svnPathsMap[project.name]}$svn_relative_path"
        val localPath="$codePath\\${project.name}\\$local_alias_path"
        val gitFullPath=project.gitFullPath()

        Shell.execute("${getPathInCP("cmd/migrate_svn_to_git.bat")} $svnPath $localPath $gitFullPath $git_branch_name")
    }






}

//fun main(args: Array<String>) {
//    var str="https://172.17.189.19/svn/RIIL_BMC_Adapter".trimEnd('/')
//    println(str)
////    if(str.endsWith('/')){
////        str=StringBuilder(str).delete(str.length-1,str.length).toString()
////    }
//    println(str.split("/").reversed().get(0))
//}